#include "StdAfx.h"

#include "UIButton.h"
#include "UIGridImgPlus.h"
#include "UIGlobalStyle.h"
#include "UIDrawTool.h"


CUIButton::CUIButton( HWND hWnd ): CUIControl( hWnd ), m_bRoundRect( FALSE ), m_bDefault( FALSE )
{
    if ( BS_OWNERDRAW != ( GetStyle() & BS_OWNERDRAW ) )
    {
        ::SetWindowLong( GetHwnd(), GWL_STYLE, GetStyle() | BS_OWNERDRAW );
    }
}

CUIButton::~CUIButton( void )
{

}

void CUIButton::OnDraw( HDC hDC )
{
    RECT rcClient;
    GetButtonRect( rcClient );

    if ( IsEnable() )
    {
        GetImage()->SetCurStatus( IMG_NORMAL );

        if ( IsHot() )
        {
            GetImage()->SetCurStatus( IMG_HOVER );
        }

        if ( IsPress() )
        {
            GetImage()->SetCurStatus( IMG_PRESS );
        }
    }
    else
    {
        GetImage()->SetCurStatus( IMG_DISABLE );
    }

    if ( GetImage() != NULL )
    {
        GetImage()->SetSize( rcClient.right, rcClient.bottom );
        GetImage()->Draw( hDC );
    }

    if ( IsFocused() )
    {
        RECT rcFocus = {0};
        ::CopyRect( &rcFocus, &rcClient );
        ::InflateRect( &rcFocus, -3, -3 );
        ::DrawFocusRect( hDC, &rcFocus );
    }

    TCHAR sText[50] = {0};
    int iLen =::GetWindowText( GetHwnd(), sText, 50 );
    ::SetBkMode( hDC, TRANSPARENT );
    ::SetTextColor( hDC, GetTextColor() );
    HFONT hOldFont = ( HFONT )::SelectObject( hDC, CUIGlobalStyle::GetFont() );
    ::DrawText( hDC, sText, iLen, &rcClient, DT_SINGLELINE | DT_CENTER | DT_VCENTER );
    ::SelectObject( hDC, hOldFont );
}

LRESULT CUIButton::ProcessUIMsg( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    return CUIControl::ProcessUIMsg( uMsg, wParam, lParam );
}
